import {defineStore} from 'pinia'
import {reqGetVideoInfo, reqGetVideoList, reqMockGetVideoList} from '@/api'
import {Video} from '@/types'

export const useVideoStore = defineStore('video', {
    state: () => ({
        videoList: <Video[]>[],
        videoSearchList: <Video[]>[],
        videoInfo: <Video>{}
    }),
    getters: {
        videoFondList: (state) => {
            let fondList: number[] = JSON.parse(<string>localStorage.getItem('fondList')) || []
            return state.videoList.filter(video => fondList.indexOf(video.id) >= 0)
        },
        videoHistoryList: (state) => {
            let videoList: Video[] = []
            let historyList: number[] = JSON.parse(<string>localStorage.getItem('historyList')) || []
            historyList.forEach(id => videoList.push(state.videoList.filter(video => video.id === id)[0]))
            return videoList
        }
    },
    actions: {
        async getVideoList() {
            // let result = await reqGetVideoList()
            let result = await reqMockGetVideoList()

            this.videoList = <Video[]>result.videoList || []

        },
        async getVideoInfo(id: number) {
            // let result = await reqGetVideoInfo(id)
            // this.videoInfo = <Video>result.video || {}
            this.videoInfo = this.videoList.filter(video => video.id == id)[0]
        },
        getVideoSearchList(keyword: string) {
            this.videoSearchList = this.videoList.filter(video => video.title.indexOf(keyword) >= 0)
        }
    }
})